iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 25
1
自我挑戰組

DevOps學習之旅系列 第 25

Day 25 GitLab 自動化測試與部署(二)

  • 分享至 

  • xImage
  •  

簡介

今天繼續研究 Gitlab 的CI/CD 設定,找了一些 python 的 .gitlab-ci.yml 範例,並且嘗試在 gitlab 上面跑.

網路範例

範例一

Using Gitlab CI Feature To Build and Test Python Flask Application and MongoDB

image: "python:3.5"
services:
  - mongo:latest
variables:
  MONGO_DB: test
before_script:
  - python --version
  - pip install virtualenv
stages:
  - build
  - test
build:
  stage: build
  script: cd schema && make && cd ../server && make
test:
  stage: test
  script: ./ci-test.sh

範例二

Using Gitlab pipelines to deploy Python packages in production and staging environments

image: python:3.6-alpine

stages:
  - deploy

before_script:
   - pip install twine
   - python setup.py sdist

deploy_staging:
  stage: deploy
  variables:
    TWINE_USERNAME: $STAGING_USERNAME
    TWINE_PASSWORD: $STAGING_PASSWORD
  script:
    - twine upload --repository-url $PYPRI_REPOSITORY_URL dist/*
  except:
    - tags

deploy_production:
  stage: deploy
  variables:
    TWINE_USERNAME: $PRODUCTION_USERNAME
    TWINE_PASSWORD: $PRODUCTION_PASSWORD
  script:
    - twine upload dist/*
  only:
    - tags

範例三

中級篇』docker之CI/CD持續集成—真實Python項目的CI演示(72)

stages:
  - style
  - test

pep8:
  stage: style
  script:
    - pip install tox
    - tox -e pep8
  tags:
    - python2.7

unittest-py27:
   stage: test
   script:
     - pip install tox
     - tox -e py27
   tags:
     - python2.7

unittest-py34:
   stage: test
   script:
     - pip install tox
     - tox -e py34
   tags:
     - python3/4

實作

---
###
# Ithome_pellok_2018 GitLab CI/CD config
###

image: python:3.5-alpine

stages:
  - test

# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache"

# Pip's cache doesn't store the python packages
# https://pip.pypa.io/en/stable/reference/pip_install/#caching
# To cache them, install in a virtualenv and cache it, too
cache:
  paths:
    - .cache/pip
    - venv/

before_script:
  - apk update && apk upgrade
  - apk add build-base
  - python -m pip install --quiet --upgrade pip
  - python --version
  - pip --version
  - pip install --quiet virtualenv
  - virtualenv venv
  - source venv/bin/activate
  - pip install --quiet -r requirement.txt


pytest_test_job:
  stage: test
  script:
    - pytest --cov

結論

Gitlab 測試都是靠設定 .gitlab-ci.yml 檔案,目前只研究到 Test 的部分.

參考

Gitlab 官網
Using Gitlab CI Feature To Build and Test Python Flask Application and MongoDB
Using Gitlab pipelines to deploy Python packages in production and staging environments


上一篇
Day 24 GitLab 自動化測試與部署(一)
下一篇
Day 26 Vagrant 構建及配置虛擬環境
系列文
DevOps學習之旅30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言